package com.bdqn.tour.util.HzySMS;


import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * packageName com.csdj.util
 * 短信发送
 * @author winver
 * @version 1.0
 * @className SMSUtil
 * @date 2024/12/16
 * @description TODO
 */

public class SMSUtil {
    //短信服务地址
    private static String url = "https://106.ihuyi.com/webservice/sms.php?method=Submit";

    /**
     * 发送短信验证码
     * @param phone  发送手机号码
     * @param code   验证码  [默认模板只能是数字验证码]
     * @return  true 成功  false  失败
     */
    public static boolean sendSms(String phone,String code){
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url);

        client.getParams().setContentCharset("UTF-8");
        method.setRequestHeader("ContentType","application/x-www-form-urlencoded;charset=GBK");
        //根据模板来确定
        String content = new String("您的验证码是：" + code + "。请不要把验证码泄露给其他人。");

        NameValuePair[] data = {//提交短信
                new NameValuePair("account", "C96099970"), //查看用户名是登录用户中心->验证码短信->产品总览->APIID
                new NameValuePair("password", "6b70addeab51ed405d8ec537950d5551"),  //查看密码请登录用户中心->验证码短信->产品总览->APIKEY
                //new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
                new NameValuePair("mobile", phone),
                new NameValuePair("content", content),
        };
        method.setRequestBody(data);
        try {
            client.executeMethod(method);
            String submitResult =method.getResponseBodyAsString();
            System.out.println(submitResult);
            Document doc = DocumentHelper.parseText(submitResult);
            Element root = doc.getRootElement();

            String rcode = root.elementText("code");
            String msg = root.elementText("msg");
            String smsid = root.elementText("smsid");

            System.out.println(rcode);
            System.out.println(msg);
            System.out.println(smsid);
            if("2".equals(rcode)){
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
        SMSUtil.sendSms("15573313721","54088");
    }
}
